//
//  NSDate+additions.swift
//  HIG
//
//  Created by arkin on 2016/10/20.
//  Copyright © 2016年 arkin. All rights reserved.
//

import UIKit

extension NSDate {
    open func yyyy(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func yyyyMM(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy-MM"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func yyyyMMdd(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy-MM-dd"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func hhmm(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "a hh:mm"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func mmddhhmmss(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "MM-dd HH:mm"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func yyddhhmmss(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy-MM-dd HH:mm"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func HHmm(from date: NSDate?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "HH:mm"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
}

extension Date {
    
    static func yyyy(from date: Date?) -> String {
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func yyyyMMdd(from date: Date?) -> String {
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy-MM-dd"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func yyyyMM(from date: Date?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy-MM"
            return dft.string(from: date! as Date)
        } else {
            return ""
        }
    }
    
    static func hhmm(from date: Date?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "a hh:mm"
            return dft.string(from: date!)
        } else {
            return ""
        }
    }
    
    static func mmddhhmmss(from date: Date?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "MM-dd HH:mm"
            return dft.string(from: date!)
        } else {
            return ""
        }
    }
    
    static func yyddhhmmss(from date: Date?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "yyyy-MM-dd HH:mm"
            return dft.string(from: date!)
        } else {
            return ""
        }
    }
    
    
    
    static func HHmm(from date: Date?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "HH:mm"
            return dft.string(from: date!)
        } else {
            return ""
        }
    }
    
    static func MMdd(from date: Date?) -> String {
        if date?.timeIntervalSince1970 == 0 {
            return ""
        }
        if let _ = date {
            let dft = DateFormatter()
            dft.dateFormat = "MM-dd"
            return dft.string(from: date!)
        } else {
            return ""
        }
    }
    
    /// 根据秒获取时间字符串
    ///
    /// - Parameter dateSeconds: 秒
    /// - Returns: 返回 时间字符串 和 未来时间还是过去时间 -1:过去时间  1:未来时间
    static func customDateStr(dateSeconds: TimeInterval) -> (dateStr: String, dateType: Int){
        if dateSeconds <= 0 {
            return ("", 0)
        }
        var resultStr = ""
        var dateType = 0
        let dft = DateFormatter()
        dft.dateFormat = "yyyy-MM-dd"
        
        var timeDif = dateSeconds - Date().timeIntervalSince1970 //时间差
      
        let todayDate = dft.date(from: dft.string(from: Date()) ) //今天0点日期date
        let tomorrowDate = todayDate?.addingTimeInterval(24 * 60 * 60) //明天0点日期date
        let tomorrowTimeInt = tomorrowDate?.timeIntervalSince1970 ?? Double.greatestFiniteMagnitude //明天0点timeint
        let afterTomorrowDate = tomorrowDate?.addingTimeInterval(24 * 60 * 60) //后天0点日期date
        let afterTomorrowTimeInt = afterTomorrowDate?.timeIntervalSince1970 ?? Double.greatestFiniteMagnitude //后天0点timeint
        let currentYear = Calendar.current.dateComponents([Calendar.Component.year], from: Date()).year ?? 1970 //当前年份
        let currentYearFirstDayInt = dft.date(from: "\(currentYear)-01-01")?.timeIntervalSince1970 ?? Double.greatestFiniteMagnitude  //当前年份第一天timeint
        let currentYearLastDayInt = dft.date(from: "\(currentYear + 1)-01-01")?.timeIntervalSince1970 ?? Double.greatestFiniteMagnitude  //当前年份下一年开始时间timeint
        
        
        if timeDif > 0 { //未来时间
            if dateSeconds < tomorrowTimeInt{ // 大于现在时间小于明天0点时间
                resultStr = "今天" + Date.HHmm(from: Date.init(timeIntervalSince1970: dateSeconds))
            }
            else if dateSeconds >= tomorrowTimeInt && dateSeconds < afterTomorrowTimeInt { //大于明天0点小于后天0点
                resultStr = "明天" + Date.HHmm(from: Date.init(timeIntervalSince1970: dateSeconds))
            }
            else if dateSeconds >= currentYearLastDayInt{ //超过今年 显示年份
                 resultStr = Date.yyddhhmmss(from: Date.init(timeIntervalSince1970: dateSeconds))
            }
            else {
                 resultStr = Date.mmddhhmmss(from: Date.init(timeIntervalSince1970: dateSeconds))
            }
            dateType = 1
        }
        else{ //过去时间
            timeDif = -timeDif
            
            let timeMin = timeDif / 60
            let timeHour = timeMin / 60
            let timeDay = timeHour / 24
            
            if timeDif < 60 {
                resultStr = "刚刚"
            }
            else if timeMin < 60 {
                resultStr = "\(Int(timeMin))分钟前"
            }
            else if timeHour < 24 {
                resultStr = "\(Int(timeHour))小时前"
            }
            else if timeDay < 7 {
                resultStr = "\(Int(timeDay))天前"
                
            }
            else if dateSeconds < currentYearFirstDayInt{ //在今年第一天之前
                resultStr = Date.yyyyMMdd(from: Date.init(timeIntervalSince1970: dateSeconds))
            }
            else {
                resultStr = Date.MMdd(from: Date.init(timeIntervalSince1970: dateSeconds))
            }
            dateType = -1
        }
        return (resultStr, dateType)
        
    }
}
